/**
 * BrickCommand defines Brick-specific commands. It also uses Mementos to 
 * store information needed for undo().
 * @author Duc Nguyen, Sophia Soohoo
 *
 */
public abstract class BrickCommand implements Command{
    /**The model*/
	private BrickModel model;
	
	/**The memento, which stores the attributes of the model
	 * prior to the command execution*/
	private Memento memento;    
	
	
	/**
	 * Execute the command
	 * @throws 	CommandException
	 * @catch 	BrickException
	 */
	public abstract void execute() throws CommandException;
	
    /**
	 * Restores the model to the previous state based on the memento
     * @throws 	CommandException by passing it the parameters of a BrickException
     * @catches	BrickExceptions
	 */
	@Override
	public void undo() throws CommandException{
		try {
			model.restore(memento);
		} catch (BrickException e) {
			throw new CommandException(e.getMessage());
		}
	}

	/**
	 * @return the model
	 */
	public BrickModel getModel() {
		return model;
	}
	
	/**
	 * @param model the model to set
	 */
	public void setModel(BrickModel model) {
		this.model = model;
	}
	
	/**
	 * @param m  the new memento
	 */
	public void setMemento(Memento m) {
	    memento = m;
	}
	
	/**
     * @return the memento
     */
    public Memento getMemento() {
        return memento;
    }
    
    /* (non-Javadoc)
     * @see java.lang.Object#clone()
     */
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
	
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return ("\nMomento: " + memento.toString());
    }	
    
}
